package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.entity.Clazz;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.entity.Student;
import com.itheima.pojo.dto.StudentQuery;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public PageResult page(StudentQuery studentQuery) {
        Page<Student> page = Page.of(studentQuery.getPage(), studentQuery.getPageSize());
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(studentQuery.getName()!=null&& !studentQuery.getName().isEmpty(),Student::getName,studentQuery.getName())
                .eq(studentQuery.getDegree()!=null,Student::getDegree,studentQuery.getDegree())
                .eq(studentQuery.getClazzId()!=null,Student::getClazzId,studentQuery.getClazzId())
                .orderByDesc(Student::getUpdateTime);
        this.page(page,wrapper);
        List<Student> students = page.getRecords();
        List<Clazz> clazzes = clazzMapper.selectList(new LambdaQueryWrapper<Clazz>().select(Clazz::getId, Clazz::getName));

        Map<Integer, String> map = clazzes.stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
        for (Student student : students) {
            student.setClazzName(map.get(student.getClazzId()));
        }

        return new PageResult(page.getTotal(),page.getRecords());
        /*PageHelper.startPage(studentQuery.getPage(),studentQuery.getPageSize());
        List<Student> stuList = studentMapper.page(studentQuery);
        Page<Student> pageList = (Page<Student>) stuList;
        return new PageResult(pageList.getTotal(),pageList.getResult());*/
    }

    @Override
    public void delete(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void saveStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        save(student);
    }

    @Override
    public Student findById(Integer id) {
        return getById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    @Transactional
    @Override
    public void violation(Integer id, Integer score) {
        Student student = getById(id);
        student.setViolationCount(student.getViolationCount()+1);
        student.setViolationScore(student.getViolationScore()+score);
        updateById(student);
    }
}
